programming4us
           
 
 
Programming

Software Testing with Visual Studio Team System 2008 : Unit testing web services & Code coverage unit test

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/15/2011 11:26:06 AM

Unit testing web services

Web services are similar to any other method in the web application. We have to just add the reference of the web service and call the method, as we call any other method of the web application. All the methods and attributes required for testing the web service are available under the namespace Microsoft.VisualStudio.TestTools.UnitTesting.Web.

Similar to the web application, the web service can be hosted using IIS server or the local development server. If it is hosted in the IIS, just add the reference to the unit test application and call the web service method just like calling the method in the web application. If it is available in the local machine but not hosted in the web server, just add the attributes required to start the development server. This attributes takes care of hosting the service locally.

Let us create a simple web service which returns values based on the input parameter value that is passed to the web method. Given below is the sample code:

public class Service1 : System.Web.Services.WebService
unit testingweb services{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetuserDetails(int id)
{
string name = "";
if (id == 100)
name = "Subha";
if (id == 200)
name = "Satheesh";
return name;
}
}

After creating the web method, right-click on the method and select Create Unit Tests…. This will create the unit test with the attributes required for the web service test.

[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\WebService1", "/")]
[UrlToTest("http://localhost:15558/")]
public void GetuserDetailsTest()
{
Service1 target = new Service1();
int id = 100;
string expected = "User Name";
string actual;
actual = target.GetuserDetails(id);
Assert.AreEqual(expected, actual);
}



The above unit test method for the web method is very similar to the method for the ASP.NET application. This web service runs in a local development server. If it has to run under the IIS server, we can change the configuration settings.

This is the property set for the Web Service project. The servers are by default set as Visual Studio Development Server. To change it to run on the IIS server, select the second option Use IIS web server and create virtual directory. Now the if we generate the unit test, the URL used for the unit test would be http://localhost/WebService1.

Just run the test similar to the other test by changing the expected value and using the required assert methods. Sometimes the test may fail because of unavailability of the web server or the server may not be running. In that case we can use the TryUrlRedirection method of the WebServiceHelper to try connecting to the URL before testing. The sample code for redirection would be

Assert.IsTrue(WebServiceHelper.TryUrlRedirection(target,
testContextInstance,
"MyServer"
),"Redirection failed."
);

Code coverage unit test

This is to see the methods or the code has been covered by the unit test. This is the property we can set on the project level before starting the unit testing. Open the configuration file localtestrun.testrunconfig file under the solution. Select Code Coverage from the list shown on the left side. Now you can see the list of projects or artifacts to instrument options. Select the project for which the code coverage has to be turned and select apply and close the dialog.

Now select the test and run the test again. Once the test is complete we can see the code coverage details from the code coverage window which can be opened using the option in the Test menu option.

The code coverage window shows the coverage details collected from the last test run. Below is the sample of the Code Coverage Result.

There was an error during the unit test of the BtnSubmit_ClickTest() method which is why the code coverage is only 50.00%. The other details are the percentage of the coverage of the code till the test failure.

If we know the fix for the test to complete 100% coverage of the method, we can just right-click on the particular method and select go to source code, which will takes us to the source code of the particular method. We can fix the code and rerun the test until the code is completely covered by the test. To get more details on the code coverage, add additional columns or remove existing columns to the output window.

Other -----------------
- .NET Debugging : Introduction to the Tools - SOS & SOSEX
- .NET Debugging : CLR 4.0 - Synchronization & Interoperability
- iPhone Programming : Connecting to the Network - Getting Data from the Internet
- iPhone Programming : Connecting to the Network - Sending Email
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Check Results
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Read and Write Files
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Get Dates and Times
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Work with Text
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 2)
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 1) - Physical Constraints
- Parallel Programming with Microsoft Visual Studio 2010 : Task Parallelism - Unhandled Exceptions in Tasks
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Tasks
- jQuery 1.3 : DOM Manipulation - Moving elements
- .NET Debugging : Introduction to the Tools - .NET 2.0—Redistributable & .NET 2.0—SDK
- .NET Debugging : Managed Heap and Garbage Collection
- Context and Interception : Custom Component Services (part 3) - The Transaction Management Service
- Context and Interception : Custom Component Services (part 2) - The Logbook Service
- Context and Interception : Custom Component Services (part 1) - Building a Custom Context Attribute & Installing a Custom Message Sink
- Software Testing with Visual Studio Team System 2008 : Data-driven unit testing
- Software Testing with Visual Studio Team System 2008 : Unit testing an ASP.NET application
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us